/** 
*
* @author  jiwf 
* @date 创建时间：2016年10月30日 下午5:33:40 
* @version 1.0 
* @since  
*  
*/
package com.xbstar.web;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.sql.DataSource;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xbstar.bean.AffixInfo;
import com.xbstar.bean.ConfigInfo;
import com.xbstar.bean.RouteInfo;
import com.xbstar.bean.UserInfo;
import com.xbstar.dao.AffixInfoRepository;
import com.xbstar.dao.ConfigInfoRepository;
import com.xbstar.dao.DestinationRepository;
import com.xbstar.dao.RouteInfoRepository;
import com.xbstar.dao.UserInfoRepository;
import com.xbstar.tools.BeanUtils;

@RestController
@RequestMapping("/phone")
public class PhoneController {
	@Autowired
	RouteInfoRepository routeRe;
	@Autowired
	UserInfoRepository userRe;
	@Autowired
	DestinationRepository destRe;
	@Autowired
	ConfigInfoRepository configRe;
	@Autowired
	AffixInfoRepository affixRe;
	@Autowired
	DataSource dataSource;
	@RequestMapping("/home")
	public String getHome() throws Exception{
		JSONObject json=new JSONObject();
		JSONObject tjson=new JSONObject();
		//0.获得配置
		List<ConfigInfo>configArr=(List<ConfigInfo>) configRe.findAll();
		if(configArr.size()>0){
			List<AffixInfo>affixArr=affixRe.findALLByBelongTo(configArr.get(0).getAffixBelongTo());
	    	if(affixArr.size()>0){
	    		tjson.put("welcomeUrl", affixArr.get(0).getUrl());
	    	}
	    	tjson.put("welcomeStr", configArr.get(0).getWelcomeStr());
		}
		//1.获得routes
		List<RouteInfo>routes= (List<RouteInfo>) routeRe.findAll();
		JSONArray routeArr=new JSONArray();
		Connection conn  = dataSource.getConnection();
		routes.stream().forEach(route->{
			JSONObject way=new JSONObject();
			way=BeanUtils.getAJsonFromObj(route, null);
			UserInfo user= userRe.findOne(route.getLeaderId());
			if(user!=null){
				way.put("headImgUrl", user.getHeadImgUrl()==null?"11":user.getHeadImgUrl());
			}
			else{
				way.put("headImgUrl", "11");
			}
			List<AffixInfo>affixArr=affixRe.findALLByBelongTo(route.getAffixBelongTo());
	    	if(affixArr.size()>0){
	    		way.put("url", affixArr.get(0).getUrl()==null?"11":affixArr.get(0).getUrl());
	    	}else{
	    		way.put("url", "11");
	    	}
	    	try{
				 String orderStr="select COUNT(*) from user_order WHERE route_info_id=?";
				 PreparedStatement ps = conn.prepareStatement(orderStr);
				 ps.setLong(1, route.getId());
				 ResultSet rs =ps.executeQuery();
				 while (rs.next()) {
					 way.put("orderNum",  rs.getString(1));
	                }
				  rs.close();
	              ps.close();
			 }catch(Exception e){
				e.printStackTrace(); 
			 }
			routeArr.put(way);
		});
		tjson.put("routeArr", routeArr);
		//2.获得destarr
		JSONArray destArr=new JSONArray();
		String getAffixStr="SELECT id,cityName,country,description,indexFor,url,province,affixBelongTo FROM destaffixview ORDER BY indexFor";
		 try {
				PreparedStatement ps = conn.prepareStatement(getAffixStr);
				ResultSet rs = ps.executeQuery();
				ArrayList<String>aa=new ArrayList<>();
				  while (rs.next()) {
					  JSONObject dest=new JSONObject();
					  dest.put("id", rs.getString("id"));
					  if(aa.contains(rs.getString("id"))){
						  continue;
					  }
					  aa.add(rs.getString("id"));
					  dest.put("url", rs.getString("url")==null?"11":rs.getString("url"));
					  dest.put("cityName", rs.getString("cityName"));
					  dest.put("country", rs.getString("country"));
					  dest.put("description", rs.getString("description"));
					  dest.put("indexFor", rs.getString("indexFor"));
					  dest.put("province", rs.getString("province"));
					  dest.put("affixBelongTo", rs.getString("affixBelongTo"));
					  destArr.put(dest);
	                }
				  rs.close();
	              ps.close();
			} catch (Exception e) {	
				e.printStackTrace();
			}
		 try{
			 conn.close();
		 } catch (Exception e) {
			e.printStackTrace();
		 }
		 tjson.put("destArr", destArr);
		 json.put(UserPackage.STATUS, "200");
		 json.put(UserPackage.DATA, tjson);
		 return json.toString();
	}
}
